package com.xueke.crawler.helper;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

import org.jsoup.Connection;
import org.jsoup.helper.HttpConnection;
/**
 * Created by weibo on 2017/8/10.
 *
 * @author weibo.qin
 * @version 2017/8/10
 */
public class HTTPCommonUtil {
    public static void trustEveryone() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[] { new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            } }, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getHttpHeaders(URL url, int timeout) {
        try {
            trustEveryone();
            Connection conn = HttpConnection.connect(url);
            conn.timeout(timeout);
            conn.header("Accept-Encoding", "gzip,deflate,sdch");
            conn.header("Connection", "close");
            conn.get();
            //String result=conn.response().body();
            Map<String, String> result = conn.response().headers();
            result.put("title", conn.response().parse().title());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getStringBody(String url, int timeout,Map<String,String> cookies,String ip) {
        try {
            trustEveryone();
            Connection conn = HttpConnection.connect(url);
            conn.timeout(timeout);
            conn.header("Accept-Encoding", "gzip,deflate,sdch");
            conn.header("X-Forwarded-For", ip);
            conn.header("Connection", "close");
            conn.cookies(cookies);
            conn.ignoreContentType(true);
            conn.followRedirects(false);
            conn.get();
            return conn.response().body();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Connection.Response getResponse(String url, int timeout, Map<String,String> cookies, String ip) {
        try {
            trustEveryone();
            Connection conn = HttpConnection.connect(url);
            conn.timeout(timeout);
            conn.header("Accept-Encoding", "gzip,deflate,sdch");
            conn.header("X-Forwarded-For", ip);
            conn.header("Connection", "close");
            conn.cookies(cookies);
            conn.ignoreContentType(true);
            conn.followRedirects(false);
            return conn.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getStringBody("https://www.icbc-axa.com", 10000,null,"127.0.0.1"));

    }
}
